//
//  ShellWallertView.m
//  YGXWallert
//
//  Created by lvzhao on 2019/5/15.
//  Copyright © 2019 链动科技. All rights reserved.
//

#import "ShellWallertView.h"
#import "ShellWallertViewModel.h"
#import "ShellWallertHeadView.h"
#import "ShellWallertCell.h"
#import "LZBaseTableView.h"
#import "ShellSelectWallertView.h"

@interface ShellWallertView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) ShellWallertViewModel *viewModel;

@property (nonatomic,strong) LZBaseTableView *tableView;

@property (nonatomic,strong) ShellWallertHeadView *headView;

@property (nonatomic,strong) ShellSelectWallertView *selectWallertView;


@end

@implementation ShellWallertView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellWallertViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    //选择货币
    [self addSubview:self.selectWallertView];
    [self.selectWallertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(K_SCREENWIDTH);
        make.height.mas_equalTo(108);
    }];
    
    ShellWallertHeadView *headView = [[ShellWallertHeadView alloc] initViewModel:self.viewModel];
    headView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 352);
    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.selectWallertView.mas_bottom);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(K_SCREENWIDTH);
        make.bottom.mas_equalTo(0);
    }];
    self.tableView.tableHeaderView = headView;


    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);

        [self reloadRecordData:[x integerValue]];
    }];


    [self.tableView addHeadWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getWallertRecord:YES];
        [self.viewModel getUserAllCoin];
    }];

    [self.tableView addAutoNormalFooterWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getWallertRecord:NO];
    }];
}

- (void)reloadRecordData:(NSInteger)type{
    if(type >= 3)return;
    
    self.tableView.mj_header.state  = MJRefreshStateIdle;
    self.tableView.mj_footer.state  = MJRefreshStateIdle;
    dismiss(nil);
    [self.tableView reloadData];
    
    switch (type) {
        case 0:{
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
            break;
        default:
            break;
    }
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShellWallertCell *wallertCell = [tableView dequeueReusableCellWithIdentifier:@"ShellWallertCell"];
    wallertCell.indexPath = indexPath;
    if(self.viewModel.dataArray.count > indexPath.row){
        wallertCell.modelObject = self.viewModel.dataArray[indexPath.row];
    }
    return wallertCell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = COLOR_BACKGROUND;
    sectionView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 50);
    //数量
    UILabel *numberLab = [UILabel lz_labelWithText:LZLocalizedString(@"数量") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
    numberLab.frame = CGRectMake(15, 15, 60, 20);
    [sectionView addSubview:numberLab];

    //类型
    UILabel *typeLabel = [UILabel lz_labelWithText:LZLocalizedString(@"类型") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
    typeLabel.frame = CGRectMake(85, 15, 60, 20);
    [sectionView addSubview:typeLabel];
    
    //状态
    UILabel *statusLabel = [UILabel lz_labelWithText:LZLocalizedString(@"状态") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
    statusLabel.frame = CGRectMake(150, 15, 50, 20);
    [sectionView addSubview:statusLabel];

    UILabel *timeLabel = [UILabel lz_labelWithText:LZLocalizedString(@"时间") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
    timeLabel.frame = CGRectMake(K_SCREENWIDTH - 15 - 50, 15, 50, 20);
    [timeLabel lz_rightAlignment];
    [sectionView addSubview:timeLabel];
    return sectionView;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}


#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ShellWallertCell class] forCellReuseIdentifier:@"ShellWallertCell"];
        _tableView.rowHeight = 50;
    }
    return _tableView;
}


- (ShellSelectWallertView *)selectWallertView{
    if(!_selectWallertView){
        _selectWallertView = [[ShellSelectWallertView alloc] initViewModel:self.viewModel];
    }
    return _selectWallertView;
}
@end
